<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Orders</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/lib/layui/css/layui.css" media="all">
</head>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div style="margin: 10px">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">车次号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="trainNumber" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">行程日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="departureTime" class="layui-input" id="departureTime"
                                   placeholder="yyyy-MM-dd">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">乘车站</label>
                        <div class="layui-input-inline">
                            <input type="text" name="startStation" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">乘车人</label>
                        <div class="layui-input-inline">
                            <input type="text" name="fullName" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">身份证号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="iDNumber" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="phone" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">订单状态</label>
                        <div class="layui-input-inline">
                            <select name="status" lay-filter="status">
                                <option value="">请选择</option>
                                <option value="正常">正常</option>
                                <option value="已退票">已退票</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">业务员</label>
                        <div class="layui-input-inline">
                            <input type="text" name="userName" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-primary" lay-submit lay-filter="search-btn"><i
                                class="layui-icon"></i> 搜 索
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <c:if test="${type == 2}">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
                        <i class="fa fa-plus"></i>
                        售票
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="update">
                        <i class="fa fa-pencil"></i>
                        退票
                    </button>
                </c:if>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/static/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        var laydate = layui.laydate;
        // 渲染
        laydate.render({
            elem: '#departureTime'
        });
        //表格渲染
        table.render({
            elem: '#currentTableId',
            url: '${pageContext.request.contextPath}/orders/query',
            contentType: 'application/json',
            method: "post",
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            page: true,
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'orderID', width: 80, title: '订单id'},
                {field: 'trainNumber', title: '车次'},
                {field: 'startStation', title: '乘车站'},
                {field: 'departureTime', title: '乘车日期'},
                {field: 'fullName', title: '乘车人'},
                {field: 'iDNumber', title: '身份证号'},
                {field: 'phone', title: '手机号'},
                {field: 'totalPrice', title: '票价'},
                {field: 'status', title: '状态'},
                {field: 'userName', title: '业务员'},
                {field: 'orderDate', title: '下单时间'},
            ]],
            response: {
                statusName: 'code', //规定返回的状态码字段为code
                statusCode: 200 //规定成功的状态码味200
            },
            skin: 'line'
        });

        //监听搜索操作
        form.on('submit(search-btn)', function (data) {
            var field = data.field;
            var param = {};
            $.each(field, function (key, value) {
                if ($.trim(value).length !== 0) {
                    param[key] = value;
                }
            });
            //执行搜索重载
            table.reload('currentTableId', {
                contentType: 'application/json',
                where: param
            }, 'data');
            return false;
        });

        //toolbar事件监听
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   //监听添加操作
                var index = layer.open({
                    title: '新增',
                    type: 2,
                    shade: 0.2,
                    shadeClose: false,
                    area: ['50%', '600px'],
                    content: 'orders/detail',
                    end: function () {
                        table.reload('currentTableId');
                    }
                });
            } else if (obj.event === 'update') {  //监听修改操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg("请选择一张订单退票", {time: 1000});
                    return;
                }
                layer.confirm('确定要退票吗？', {
                    btn: ['确定', '关闭'] //按钮
                }, function () {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/orders/refund/"+ data[0].orderID,
                        type: "POST",
                        dataType: 'json',
                        success: function (data) {
                            layer.msg(data.msg, {time: 500}, function () {
                                table.reload("currentTableId");
                            });
                        }
                    });

                }, function () {

                });


            }
        });
    });
</script>
</body>
</html>
